package com.lzy.concertticket.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.concertticket.dao.ArtistDao;
import com.lzy.concertticket.model.entity.ArtistEntity;
import com.lzy.concertticket.model.req.ArtistReq;
import com.lzy.concertticket.model.vo.ArtistVo;
import com.lzy.concertticket.service.ArtistService;
import com.lzy.concertticket.threadlocal.ResponseThreadLocal;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
* @author 30793
* @description 针对表【tb_artist(艺人信息表)】的数据库操作Service实现
* @createDate 2025-08-16 23:21:21
*/
@Service
public class ArtistServiceImpl implements ArtistService{

    @Autowired
    private ArtistDao artistDao;

    @Override
    public boolean addArtist(ArtistReq artistReq) {
        //  检查明星是否存在
        ArtistEntity artistDb = artistDao.getByName(artistReq.getName());
        Assert.isNull(artistDb, "该明星已存在");
        artistDao.save(artistReq);
        return true;
    }

    @Override
    public ArtistVo getArtistInfo(ArtistReq artistReq) {
        return artistDao.getByIdOrName(artistReq.getId(), artistReq.getName());
    }

    @Override
    public Page<ArtistVo> getArtistPage(ArtistReq artistReq) {
        return artistDao.getPage(artistReq);
    }

    @Override
    public void updateArtist(ArtistReq artistReq) {
        artistDao.updateById(artistReq);
    }

    @SneakyThrows
    @Override
    public void export() {
        //  查出所有明星数据
        List<ArtistEntity> artistList = artistDao.list();
        //  获取响应对象
        HttpServletResponse response = ResponseThreadLocal.get();
        //  设置响应excel格式
        response.setContentType("application/vnd.ms-excel");
        //  获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //  导出到 Excel
        EasyExcel.write(outputStream, ArtistEntity.class).sheet().doWrite(artistList);
    }

    @Override
    public void deleteArtist(Long artistId) {
        artistDao.removeById(artistId);
    }
}




